
#include <BFont>
#include <BStyle>
#include <BWidget>
#include "member_BLineEdit.h"

using namespace BWE;

member_BLineEdit::member_BLineEdit(BLineEdit *edit)
{
	boss = edit;
	align = Align_LeftCenter;
	spacing = 1;
	prefixAlign = Align_RightCenter;
	suffixAlign = Align_LeftCenter;

	readOnly = false;
	blinkTime = 1;
	blinkTick = 0;
	prefixStyle = 0;
	suffixStyle = 0;
	lineWidth = 1.0f;

	dirty = true;
	editing = false;
	pressed = false;
	hoverIndex = 0;
	blinkShown = false;
}
member_BLineEdit::~member_BLineEdit()
{
}

void member_BLineEdit::emitEdited()
{
	editing = true;
	dirty = true;
	if (boss->checks(Signal_TextEdited))
		boss->emit(Signal_TextEdited, block.text);
	if (boss->checks(Signal_TextChanged))
		boss->emit(Signal_TextChanged, block.text);
	boss->fresh();
}

bool member_BLineEdit::freshSizes()
{
	const BStyle *style = boss->realStyle();
	const BFont *font = boss->realFont();

	prefixSize.reset();
	if (prefix.size())
	{
		if (prefixStyle)
			prefixStyle->translate(prefix, t_prefix);
		else
			style->translate(prefix, t_prefix);
		const BFont *prefixFont = 0;
		if (prefixStyle)
			prefixFont = prefixStyle->value(Value_Font);
		if (!prefixFont)
			prefixFont = font;
		if (t_prefix.size())
			prefixSize = prefixFont->textSize(t_prefix);
		else
			prefixSize = prefixFont->textSize(prefix);
	}

	suffixSize.reset();
	if (suffix.size())
	{
		if (suffixStyle)
			suffixStyle->translate(suffix, t_suffix);
		else
			style->translate(suffix, t_suffix);
		const BFont *suffixFont = 0;
		if (suffixStyle)
			suffixFont = suffixStyle->value(Value_Font);
		if (!suffixFont)
			suffixFont = font;
		if (t_suffix.size())
			suffixSize = suffixFont->textSize(t_suffix);
		else
			suffixSize = suffixFont->textSize(suffix);
	}
	return true;
}
void member_BLineEdit::freshRects()
{
	BRect crect = boss->centerRect();
	const BFont *font = boss->realFont();

	textArea = crect;
	textArea.left() += prefixSize.width() + spacing;
	textArea.right() -= suffixSize.width() + spacing;

	block.font = font;
	block.dirty = true;
	block.adjust(0);

	BRect textRect = textArea.align(align, block.textSize);
	block.rect = textRect;

	BRect prefixArea = crect;
	prefixArea.right() = block.rect.left() - spacing;
	prefixRect = prefixArea.align(prefixAlign, prefixSize);

	BRect suffixArea = crect;
	suffixArea.left() = block.rect.right() + spacing;
	suffixRect = suffixArea.align(suffixAlign, suffixSize);
}
BLine2 member_BLineEdit::inputLine()
{
	BLine2 line;
	if (boss->focused())
	{
		if (block.cells.check(block.index))
		{
			BRect cellRect = block.cells[block.index].rect + block.rect.pos();
			line.setBegin(cellRect.left(), block.rect.top());
			line.setEnd(cellRect.left(), block.rect.bottom());
		}
		else
		{
			BRect cellRect = block.cells.last().rect + block.rect.pos();
			line.setBegin(cellRect.right(), block.rect.top());
			line.setEnd(cellRect.right(), block.rect.bottom());
		}
		if (!textArea.contain(line.begin()))
		{
			BPoint point = textArea.wrap(line.begin());
			BPoint offset = point - line.begin();
			block.rect.move(offset.x(), 0);
			line.move(offset.x(), 0);
		}
	}
	return line;
}
